<?php
namespace App\Http\Controllers\Api\LearnWord;
use App\Enums\Api\Response;
use App\Http\Controllers\Controller;
use App\Models\Category\CategoryModule;
use App\Models\Tool\FileUrl;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Data\ClickLearn;
use Illuminate\Http\JsonResponse;

class LearnWordController extends Controller
{
    public function index(int $gradeId):JsonResponse
    {
        if ( !ClickLearn::query()->has('learn_word')->whereHas('learn_word',function (Builder $query)use($gradeId){
            $query->where('grade_id',$gradeId);
        })->exists() ) {
            return apiResponse(code:Response::SYSTEM_DATA_NOT_EXISTS->value,data:[],msg: Response::SYSTEM_DATA_NOT_EXISTS->response(),type: 'learn_word');
        }
        $file_url=FileUrl::query()->where('module',CategoryModule::ModuleTypeFour)->first();
        $learn_work_list=ClickLearn::query()->with('sentence_translate:id,translation,content,audio_url,image_url')->has('learn_word')->whereHas('learn_word',function (Builder $query)use($gradeId){
            $query->where('grade_id',$gradeId);
        })->get()->toArray();
        $word_list=array_map(function ($item)use($file_url,$gradeId){
            $value['id']=$item['id'];
            $value['type']=$this->type_convert($item['type']);
            $value['content']=$item['content'];
            $value['sentence']=$item['word_blanks'];
            $value['translation']=$item['translation'];
            $value['soundmark']=$item['phonetic'];
            $value['audioName']=convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address: $item['audio_url']??'',grade:$gradeId,category:'',type: '.mp3',file_url: $file_url);
            $value['exampleSentence']=$item['sentence'];
            $value['exampleTranslation']=$item['sentence_translation'];
            $value['image_url']=convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address:$item['image_url']??'',grade:$gradeId,category:'',type: '.png',file_url: $file_url);
            if ($item['sentence_translate']==null){
                $value['sentence_translate']=[];
            }else{
                $value['sentence_translate']['content']=$item['sentence_translate']['content'];
                $value['sentence_translate']['image_url']=convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address:$item['sentence_translate']['image_url']??'',grade:$gradeId,category:'',type: '.png',file_url: $file_url);
                $value['sentence_translate']['translation']=$item['sentence_translate']['translation'];
                $value['sentence_translate']['audioName']=convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address: $item['sentence_translate']['audio_url']??'',grade:$gradeId,category:'',type: '.mp3',file_url: $file_url);
            }
            return $value;
        },$learn_work_list);
        return apiResponse(code:Response::SERVICE_SUCCESS->value,data:$word_list,msg: Response::SERVICE_SUCCESS->response(),type: 'learn_word');
    }
    protected function type_convert(int $type): string
    {
        return match ($type) {
            ClickLearn::TYPE_SENTENCE => 'sentence',
            ClickLearn::TYPE_WORD => 'word',
            ClickLearn::TYPE_WORD_GROUP => 'wordgroup',
            default => '',
        };
    }
}
